package com.example.administrator.xiaobo.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.adapter.LiveFragmentPageAdapter;
import com.example.administrator.xiaobo.fragment.LiveDetailsFragment.GameFragment;
import com.example.administrator.xiaobo.fragment.LiveDetailsFragment.MovieFragment;
import com.example.administrator.xiaobo.fragment.LiveDetailsFragment.ShowFragment;
import com.example.administrator.xiaobo.fragment.LiveDetailsFragment.VarietyFragment;

import java.util.LinkedList;

/**
 * A simple {@link Fragment} subclass.
 */
public class LiveFragment extends Fragment implements TabLayout.OnTabSelectedListener {
    public static final String TAG  = "LiveFragment";
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private LinkedList<Fragment> data;
    private String[] titles;
    private View view;
    private LiveFragmentPageAdapter adapter;
    private ImageView imageView;//排行榜
    public LiveFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view  = LayoutInflater.from(getContext()).inflate(R.layout.fragment_live,null);
        init(view);
        loadData();
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(this);
    }

    private void loadData() {
        GameFragment gameFragment = new GameFragment();
        MovieFragment movieFragment = new MovieFragment();
        ShowFragment showFragment = new ShowFragment();
        VarietyFragment varietyFragment = new VarietyFragment();
        data.add(showFragment);
        data.add(gameFragment);
        data.add(movieFragment);
        data.add(varietyFragment);
    }

    private void init(View view) {
        tabLayout = (TabLayout) view.findViewById(R.id.tl);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        data = new LinkedList<>();
        titles = new String[getResources().getStringArray(R.array.titles).length];
        titles = getResources().getStringArray(R.array.titles);
        imageView = (ImageView) view.findViewById(R.id.charts);
        //初始化tablayout
        tabLayout.setTabTextColors(Color.parseColor("#353535"),Color.parseColor("#353535"));
        //设置小滑块的高度
        tabLayout.setSelectedTabIndicatorHeight(5);
        //设置小滑块的颜色
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#353535"));
        adapter = new LiveFragmentPageAdapter(getChildFragmentManager(),data,titles);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view;
    }

    //tablayout的监听事件
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition()!=titles.length) {
            viewPager.setCurrentItem(tab.getPosition());
            if (tab.getPosition()>=titles.length/2){
                imageView.setVisibility(View.GONE);
            }else{
                imageView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
